

local MakePlayerCharacter = require 'prefabs/player_common'

local avatar_name = 'yuemiheng'
local assets = {
	Asset('SCRIPT', 'scripts/prefabs/player_common.lua'),
	Asset('ANIM', 'anim/'..avatar_name..'.zip'),
	Asset('ANIM', 'anim/ghost_'..avatar_name..'_build.zip'),
}

local prefabs = {}

local start_inv = {}
-- for k, v in pairs(TUNING.GAMEMODE_STARTING_ITEMS) do
-- 	start_inv[string.lower(k)] = v[string.upper(avatar_name)]
-- end
start_inv['default'] = {}
for k,v in pairs(TUNING.YUEMIHENG_CUSTOM_START_INV) do
	for i = 1, v.num do 
		table.insert(start_inv['default'], k)
	end
end

prefabs = FlattenTree({ prefabs, start_inv }, true)
---------------------------------------------------------------------------
---------------------------------------------------------------------------
local function onbecamehuman(inst, data, isloading)
	inst.components.locomotor:SetExternalSpeedMultiplier(inst, avatar_name..'_speed_mod', TUNING.MOD_YUEMIHENG.AVATAR.YUEMIHENG.WALKSPEEDMULT)
end

local function onbecameghost(inst, data)
	inst.components.locomotor:RemoveExternalSpeedMultiplier(inst, avatar_name..'_speed_mod')
end
---------------------------------------------------------------------------
---------------------------------------------------------------------------
local function onload(inst,data)
	inst:ListenForEvent('ms_respawnedfromghost', onbecamehuman)
	inst:ListenForEvent('ms_becameghost', onbecameghost)

	if inst:HasTag('playerghost') then
		onbecameghost(inst)
	else
		onbecamehuman(inst)
	end
end
---------------------------------------------------------------------------
---------------------------------------------------------------------------
-- 主/客机
local common_postinit = function(inst)
	inst:AddTag(avatar_name)

	-- warly
	inst:AddTag("masterchef")
	inst:AddTag("professionalchef")
		
	inst:AddTag("stronggrip")
	
	inst.MiniMapEntity:SetIcon(avatar_name..'.tex')
end
-- 主机
---comment
---@param inst ent
local master_postinit = function(inst)	
	inst.starting_inventory = start_inv[TheNet:GetServerGameMode()] or start_inv.default
	inst.soundsname = 'wendy'
	
	inst.components.health:SetMaxHealth(TUNING[string.upper(avatar_name)..'_HEALTH'])
	inst.components.hunger:SetMax(TUNING[string.upper(avatar_name)..'_HUNGER'])
	inst.components.sanity:SetMax(TUNING[string.upper(avatar_name)..'_SANITY'])
	
	-- inst.components.hunger:SetRate(TUNING.MOD_YUEMIHENG.AVATAR.YUEMIHENG.HUNGERRATE) -- 饥饿速率
	inst.components.hunger.burnratemodifiers:SetModifier(inst, TUNING.MOD_YUEMIHENG.AVATAR.YUEMIHENG.HUNGERRATE, 'yuemiheng_self')

	inst:AddComponent('ymh_eat_marble')
	
	inst:DoTaskInTime(2,function()
		if inst.components.temperature then
			inst.components.temperature:SetTemperature(35)
			local old_SetTemperature = inst.components.temperature.SetTemperature
			function inst.components.temperature:SetTemperature(value,...)
				if self.inst.prefab and self.inst.prefab == avatar_name then
					return
				end
				return old_SetTemperature(self,value,...)
			end
			local old_GetCurrent = inst.components.temperature.GetCurrent
			function inst.components.temperature:GetCurrent(...)
				if self.inst.prefab and self.inst.prefab == avatar_name then
					return 35
				end
				return old_GetCurrent(self,...)
			end
		end
	end)
	
	-- 自带一本科技
    inst.components.builder.science_bonus = 1
	
	-- 可以读书和写书
	inst:AddTag("bookbuilder")
	inst:AddTag("reader")
	inst:AddComponent("reader")
	
	-- 工作效率
	inst.components.workmultiplier:AddMultiplier(ACTIONS.CHOP,   TUNING.MOD_YUEMIHENG.AVATAR.YUEMIHENG.WORKEFFICIENCY, 'ymh_perks')
	inst.components.workmultiplier:AddMultiplier(ACTIONS.MINE,   TUNING.MOD_YUEMIHENG.AVATAR.YUEMIHENG.WORKEFFICIENCY, 'ymh_perks')
	inst.components.workmultiplier:AddMultiplier(ACTIONS.HAMMER, TUNING.MOD_YUEMIHENG.AVATAR.YUEMIHENG.WORKEFFICIENCY, 'ymh_perks')
	
	inst:AddComponent('ymh_lv')
	
	inst.OnLoad = onload
	inst.OnNewSpawn = onload
end
-- 人物皮肤
local function MakeYUEMIHENGSkin(name, data, notemp, free)
	local d = {}
	d.rarity = '典藏'
	d.rarityorder = 2
	d.raritycorlor = { 0 / 255, 255 / 255, 249 / 255, 1 }
	d.release_group = -1001
	d.skin_tags = { 'BASE', avatar_name, 'CHARACTER' }
	d.skins = {
		normal_skin = name,
		ghost_skin = 'ghost_'..avatar_name..'_build'
	}
	if not free then
		d.checkfn = YUEMIHENG_API.YUEMIHENGSkinCheckFn
		d.checkclientfn = YUEMIHENG_API.YUEMIHENGSkinCheckFn
	end
	d.share_bigportrait_name = avatar_name
	d.FrameSymbol = 'Reward'
	for k, v in pairs(data) do
		d[k] = v
	end
	YUEMIHENG_API.MakeCharacterSkin(avatar_name, name, d)
	if not notemp then
		local d2 = shallowcopy(d)
		d2.rarity = '限时体验'
		d2.rarityorder = 80
		d2.raritycorlor = { 0.957, 0.769, 0.188, 1 }
		d2.FrameSymbol = 'heirloom'
		d2.name = data.name .. '(限时)'
		YUEMIHENG_API.MakeCharacterSkin(avatar_name, name .. '_tmp', d2)
	end
end
function MakeYUEMIHENGFreeSkin(name, data)
	MakeYUEMIHENGSkin(name, data, true, true)
end

MakeYUEMIHENGFreeSkin(avatar_name..'_none', {
	name = 'yuemiheng', -- 皮肤的名称
	des = '*\n*', -- 皮肤界面的描述
	quotes = '\'\'', -- 选人界面的描述
	rarity = '典藏', -- 珍惜度 官方不存在的珍惜度则直接覆盖字符串
	rarityorder = 2, -- 珍惜度的排序 用于按优先级排序 基本没啥用
	raritycorlor = { 189 / 255, 73 / 255, 73 / 255, 1 }, -- {R,G,B,A}
	skins = { normal_skin = avatar_name, ghost_skin = 'ghost_'..avatar_name..'_build' },
	build_name_override = avatar_name,
	share_bigportrait_name = avatar_name..'_none',
})


return MakePlayerCharacter(avatar_name, prefabs, assets, common_postinit, master_postinit, prefabs)


